<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Biji extends Model{
	/**
	 * 关联到模型的数据表
	 *
	 * @var string
	 */
	protected $table = 'dqm_biji';

	/**
	 * 字段：笔记唯一标识
	 */
	const FIELD_BIJI_ID = 'biji_id';

	/**
	 * 字段：用户唯一标识
	 */
	const FIELD_USER_ID = 'user_id';

	/**
	 * 字段：标签唯一标识
	 */
	const FIELD_TAG_ID = 'tag_id';

	/**
	 * 字段：笔记唯一标识
	 */
	const FIELD_BOOK_ID = 'book_id';

	/**
	 * 字段：笔记标题
	 */
	const FIELD_BIJI_TITLE = 'biji_title';

	/**
	 * 字段：笔记内容
	 */
	const FIELD_BIJI_CONTENT = 'biji_content';

	/**
	 * 字段：标记删除，1、标记删除 ; 0、正常
	 */
	const FIELD_TRASH = 'trash';

	/**
	 * 字段：标记分享，1、分享 ; 0、私人
	 */
	const FIELD_SHARE = 'share';

	/**
	 * 字段：创建时间
	 */
	const FIELD_CREATED_TIME = 'created_time';

	/**
	 * 字段：更新时间
	 */
	const FIELD_UPDATED_TIME = 'updated_time';

	/**
	 * 笔记删除状态: 1、标记删除 ; 0、正常
	 */
	const ENABLE_TRASH_STATUS = 1;
	const DISABLE_TRASH_STATUS = 0;


	/**
	 * 笔记分享状态: 1、分享 ; 0、私人
	 */
	const ENABLE_SHARE_STATUS = 1;
	const DISABLE_SHARE_STATUS = 0;

	/**
	 * The attributes that are mass assignable.
	 *
	 * @var array
	 */
	protected $fillable = [
		self::FIELD_BIJI_TITLE, self::FIELD_BIJI_CONTENT, self::FIELD_TRASH, self::FIELD_SHARE
	];

	/**
	 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
	 */
	public function user(){
		return $this->belongsTo('App\User');
	}

	/**
	 * 获取指定用户下的所有正常(无被标记删除)的笔记信息，并按指定排序方式排序
	 *
	 * @param $user_id
	 * @param $sort_type
	 * @param array $select_fields
	 * @return mixed
	 */
	static public function getBijisByUserId($user_id, $sort_type, $select_fields = []){
		$condition['where'] = array(self::FIELD_TRASH=>self::DISABLE_TRASH_STATUS, self::FIELD_USER_ID=>$user_id);
		switch ($sort_type) {
			case 'earliest_created':
				$condition['order_by'] = array(self::FIELD_CREATED_TIME, 'asc');
				break;
			case 'newest_created' :
				$condition['order_by'] = array(self::FIELD_CREATED_TIME, 'desc');
				break;
			case 'earliest_updated':
				$condition['order_by'] = array(self::FIELD_UPDATED_TIME, 'asc');
				break;
			case 'newest_updated':
				$condition['order_by'] = array(self::FIELD_UPDATED_TIME, 'desc');
				break;
			case 'asc_title':
				$condition['order_by'] = array(self::FIELD_BIJI_TITLE, 'asc');
				break;
			case 'desc_title':
				$condition['order_by'] = array(self::FIELD_BIJI_TITLE, 'desc');
				break;
			default:
				$condition['order_by'] = array(self::FIELD_CREATED_TIME, 'asc');
		}
		$result = Biji::select($select_fields)->where($condition['where'])->orderBy($condition['order_by'][0], $condition['order_by'][1])->get();
		return $result;
	}

	/**
	 * 根据条件获取笔记数量
	 *
	 * @param array $conditions
	 * @return int
	 */
	static public function getBijiCountByConditions($conditions = []){
		$result = Biji::where($conditions['where'])->count();
		return $result;
	}

	/**
	 * 根据查询条件获取正常状态的笔记列表
	 *
	 * @param array $conditions
	 * @param array $select_fields
	 * @return mixed
	 */
	static public function getNormalListByConditions($conditions = [], $select_fields = []){
		$condition['where'][self::FIELD_TRASH] = self::DISABLE_TRASH_STATUS;
		$result = Biji::select($select_fields)->where($conditions['where'])->get();
		return $result;
	}

}
